<?php
/*
 * @Author: your name2
 * @Date: 2021-09-21 16:46:35
 * @LastEditTime: 2022-03-10 17:27:06
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /nlog/app/Http/Controllers/AdminController/Login.php
 */

namespace App\Http\Controllers\crm;


use Illuminate\Support\Facades\Request;
use App\Models\crm_model\CrmLinkman as CrmLinkmanDb;
use App\Models\crm_model\CrmLinkmanAttachment;
use App\Models\crm_model\CrmCustomer;

class CrmLinkman extends BaseController
{

    public function linkmanAdd()
    {
        if (Request::has('formData')) {
            $formData = Request::input('formData');
            $res = redis_mysql_add_return('crm_linkman', $formData);
            if($res && Request::has('information')){
               $information = Request::input('information');
               for($i=0;$i<count( $information);$i++){
                  $information[$i]['linkman_id'] = $res;
                  $data =  $information[$i];
                  go(function () use ( $data) {
                       redis_mysql_add_return('crm_linkman_information', $data)  ;
                  });
               }
            }
            if ($res  && Request::has('customer_id')) {   //修改客户联系人
                $customer_id = Request::input('customer_id');
                if (count($customer_id) > 0) {
                    $bool =  CrmCustomer::customerLinkManEdit($res, implode(",", $customer_id));
                    if ($bool) {
                        for ($i = 0; $i < count($customer_id); $i++) {
                            $id = $customer_id[$i];
                            go(function () use ($id) {
                                redis_mysql_del('crm_customer', $id);
                            });
                        }
                    }
                }
            }
            if ($res) {
                return $this->create(200, '添加成功');
            }
        }
    }

    public function linkmanEdit()
    {
        if (Request::has('formData') && Request::has('conditions')) {
            $conditions = Request::input('conditions');
            $formData = Request::input('formData');
            $res =  redis_mysql_edit_return('crm_linkman', $conditions, $formData);
            if ($res  && Request::has('customer_id')) {   //修改客户联系人
                $customer_id = Request::input('customer_id');
                $data_id = [];
                $customerFindInSet =   CrmCustomer::customerFindInSet($conditions['id']);
                if ($customerFindInSet) {
                    $data_id =  array_map(fn ($item) => $item['id'], $customerFindInSet);
                }
                CrmCustomer::customerChangeOld($conditions['id']);   //查询旧的
                if (count($customer_id) > 0) {
                    $bool =  CrmCustomer::customerLinkManEdit($conditions['id'], implode(",", $customer_id));
                    if ($bool) {
                        $data_id  = array_merge($data_id, $customer_id);     //新加入要修改的id
                    }
                }
                array_map(fn ($item) => go(function () use ($item) {
                    redis_mysql_del('crm_customer', $item);
                }), $data_id);
            }
            if ($res) {
                return $this->create(200, '编辑成功');
            }
        }
    }
    public function linkmanSelOne()
    {
        return redis_mysql_selone('crm_linkman');
    }





    public function linkmanList()
    {
        // if(Request::has('division_id')){
        $whereStr = '';
        $limitStr = '';
        $division_id =  Request::has('division_id') ? Request::input('division_id') : $this->getAdminInfo()['division_id'];

        if (Request::input('pageSize') && Request::input('page')) {
            $limitStr =   limit_joint(Request::input('page'), Request::input('pageSize'));
        }
        if (Request::has('keyWord')) {

            $keyWord = array_filter(Request::input('keyWord'), 'filtrfunction');
            if (isset($keyWord['manyField'])) {
                $whereStr .= havaWhereLikeManyField($keyWord['manyField']);
                unset($keyWord['manyField']);
            }
            if (!empty($keyWord)) {
                $whereStr .= havaWhereLikeJoinEqual($keyWord);
            }
        }
        $list  =  CrmLinkmanDb::crmLinkmanList($division_id, $whereStr, $limitStr);
        $count =  CrmLinkmanDb::crmLinkmanCount($division_id, $whereStr);
        return $this->create(200, '查询成功', ['list' => $list, 'total' =>  $count[0]['count'] ?? 0]);
    }

    public function linkmanGirdData()
    {
        $whereStr = '';
        $limitStr = '';
        if (Request::input('pageSize') && Request::input('page')) {
            $limitStr =   limit_joint(Request::input('page'), Request::input('pageSize'));
        }
        if (Request::has('keyWord')) {

            $keyWord = array_filter(Request::input('keyWord'), 'filtrfunction');
            if (isset($keyWord['manyField'])) {
                $whereStr .= havaWhereLikeManyField($keyWord['manyField'], '-', false);
                unset($keyWord['manyField']);
            }
            if (!empty($keyWord)) {
                $whereStr .= havaWhereLikeJoinEqual($keyWord,  $whereStr ? true : false);
            }
            if ($whereStr) {
                $whereStr = ' WHERE ' . $whereStr;
            }
        }
        $list = CrmLinkmanDb::linkmanGirdDataList($whereStr, $limitStr);
        return $this->create(200, '查询成功', ['list' => $list]);
    }


    public function attachmentSave()
    {
        if (Request::has('formData')) {
            $formData = Request::input('formData');
            if (isset($formData['id']) && $formData['id']) {
                $id = $formData['id'];
                unset($formData['id']);
                $formData['update_time'] = date('Y-m-d H:i:s');
                $res = CrmLinkmanAttachment::edit($formData, $id);
            } else {
                $formData['create_time'] = date('Y-m-d H:i:s');
                $res = CrmLinkmanAttachment::insert($formData);
                $situation_id = $res;
            }
            if ($res) {
                return   isset($situation_id) ? $this->create(200, '保存成功', ['id' => $situation_id]) : $this->create(200, '保存成功');
            } else {
                return   $this->create(100, '保存失败');
            }
        }
    }



    public function attachmentList()
    {
        $whereStr = '';
        $limitStr = '';
        if (Request::input('pageSize') && Request::input('page')) {
            $limitStr =   limit_joint(Request::input('page'), Request::input('pageSize'));
        }
        if (Request::has('keyWord')) {
            $keyWord = array_filter(Request::input('keyWord'), 'filtrfunction');
            if (isset($keyWord['manyField'])) {
                $whereStr .= havaWhereLikeManyField($keyWord['manyField']);
                unset($keyWord['manyField']);
            }
            if (!empty($keyWord)) {
                $whereStr .= havaWhereLikeJoinEqual($keyWord);
            }
        }
        $list = CrmLinkmanAttachment::list($whereStr, $limitStr);
        return $this->create(200, '查询成功', ['list' => $list]);
    }

    
    


    // public function linkmanAdd(){
    //     return redis_mysql_add('crm_linkman');
    // }

}
